package cn.itcast.hotel;

import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.List;


@SpringBootTest
class HotelIndexTest1 {

    private RestHighLevelClient client;

    @Autowired
    IHotelService hotelService;

    @Test
    public void test() throws IOException {
        //将数据库中 根据id查询一条数据 hotel
        Hotel hotel = hotelService.getById(60359);
        //将hotel转为hoteldoc
        HotelDoc hotelDoc = new HotelDoc(hotel);
        //创建请求对象,IndexRequest
        IndexRequest request = new IndexRequest("hotel");
        //设置id
         request.id(String.valueOf(hotelDoc.getId()));
        //设置请求参数,并指定参数类型Json
         request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
        //添加文档的方法:index
        IndexResponse response = client.index(request, RequestOptions.DEFAULT);

        System.out.println(response.getResult());
    }

    @Test
    public void getDocumentByID() throws IOException {
        //创建请求
        GetRequest hotel = new GetRequest("hotel", "60359");

        GetResponse response = client.get(hotel, RequestOptions.DEFAULT);

        String json = response.getSourceAsString();

        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);

        System.out.println(hotelDoc);
    }

    /**
     * 修改
     */
    @Test
    public void update() throws IOException {

        UpdateRequest hotel = new UpdateRequest("hotel", "60359");

        hotel.doc("price", "9998");
        UpdateResponse response = client.update(hotel, RequestOptions.DEFAULT);

        System.out.println(response.getResult());
    }

    /**
     * 删除
     */
    @Test
    public void delete() throws IOException {

        DeleteRequest request = new DeleteRequest("hotel","60359");
        DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());
    }

    /**
     * 批处理
     *
     */
    @Test
    public void bulkadd() throws IOException {
        List<Hotel> list = hotelService.list();

        BulkRequest request = new BulkRequest("hotel");

        for (Hotel hotel : list) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(String.valueOf(hotelDoc.getId()));
            indexRequest.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
            request.add(indexRequest);
        }
        BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);

        System.out.println(response.status());
    }

    @Test
    public void findAll() throws IOException {

        SearchRequest hotel = new SearchRequest("hotel");

        SearchResponse response = client.search(hotel, RequestOptions.DEFAULT);

        System.out.println(response);
    }
    @BeforeEach
    void setUp() {
        client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.200.131:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        client.close();
    }
}
